package com.example.wanglong.myoneapplication.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.LinearLayout;

import com.example.wanglong.myoneapplication.common.BaseActivity;
import com.example.wanglong.myoneapplication.R;

/**
 * Created by wang.long on 2016/4/14.
 */
public class WebViewPage extends BaseActivity implements View.OnClickListener {

    private WebView homeView;
    private LinearLayout v;
    private LinearLayout back;
    private LinearLayout home;
    private Handler mHandler = new Handler();

    private String source,type;

    @Override
    protected void onCreate(Bundle saveInstanceState){
        super.onCreate(saveInstanceState);

        super.setContentView(R.layout.otherpage);
        v =(LinearLayout) findViewById(R.id.visatitle);
        v.getBackground().setAlpha(255);
        //参数接收
        Intent intentdata=getIntent();
        source=intentdata.getStringExtra("source");
        type=intentdata.getStringExtra("type");


        homeView=(WebView) findViewById(R.id.webview);
        back=(LinearLayout) findViewById(R.id.back);
        home=(LinearLayout) findViewById(R.id.home);


        //支持Javascript
        homeView.getSettings().setJavaScriptEnabled(true);
        //优先使用缓存
        homeView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
        //中文的支持:
        homeView.getSettings().setDefaultTextEncodingName("GBK");


        homeView.setWebChromeClient(new WebChromeClient() {

            @Deprecated
            public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
                return super.onJsAlert(view, url, message, result);
            }

        });


        back.setOnClickListener(this);
        home.setOnClickListener(this);

        //初始化
        initData(source, type);

    }

    public void initData(String source,String type){
        switch (source){
            case "home":
                initType(type);
                break;

            default:
                break;
        }
    }

    public void initType(String type){
        switch (type){
            case "0":
                homeView.loadUrl("file:///android_asset/homeonehtml/hone.html");
                break;
            case "1":
                homeView.loadUrl("file:///android_asset/homeonehtml/htwo.html");
                break;
            case "2":
                homeView.loadUrl("file:///android_asset/homeonehtml/hthree.html");
                break;

            default:
                break;
        }


    }

    @Override
    public void onClick(View v){
        switch (v.getId()){
            case R.id.back:
                finish();
                break;

            case R.id.home:
                actionStart(WebViewPage.this);
                break;

            default:
                break;
        }
    }

    public static void actionStart(Context context){
        Intent intent=new Intent(context,FragmentMainActivity.class);
        context.startActivity(intent);
    }


}
